#include<bits/stdc++.h>
#define int long long
using namespace std;
namespace IO
{
	char buff[1<<21],*p1=buff,*p2=buff;
	char getch(){return p1==p2&&(p2=((p1=buff)+fread(buff,1,1<<21,stdin)),p1==p2)?EOF:*p1++;}
	template<typename T>void read(T &x){char CH=getch();int fl=1;x=0;while(CH>'9'||CH<'0'){if(CH=='-')fl=-1;CH=getch();}while(CH<='9'&&CH>='0'){x=x*10+CH-48;CH=getch();}x*=fl;}
	template<typename T>void read_s(T &x){x="";char CH=getch();while(!(CH>='a'&&CH<='z')&&!(CH>='A'&&CH<='Z'))CH=getch();while((CH>='a'&&CH<='z')||(CH>='A'&&CH<='Z')){x+=CH;CH=getch();}}
	template<typename T,typename ...Args>void read(T &x,Args& ...args){read(x);read(args...);}
	char obuf[1<<21],*p3=obuf;
	void putch(char CH) {if(p3-obuf<(1<<21))*p3++=CH;else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=CH;}
	char CH[100];
	template<typename T>void write(T x) {if(!x)return putch('0');if(x<0)putch('-'),x*=-1;int top=0;while(x)CH[++top]=x%10+48,x/=10;while(top)putch(CH[top]),top--;}
	template<typename T,typename ...Args>void write(T x,Args ...args) {write(x);putch(' ');write(args...);}
	void put(string s){for(int i=0;i<s.size();i++)putch(s[i]);}
	void flush(){fwrite(obuf,p3-obuf,1,stdout);}
}
using namespace IO;
struct node
{
	int nxt,to;
}e[100005];
int head[100005],cnt_edge;
void add_edge(int u,int v)
{
	e[++cnt_edge].to=v;
	e[cnt_edge].nxt=head[u];
	head[u]=cnt_edge;
}
const int mod=1e9+7;
int n,m;
int w[100005],a[100005];
int f[100005],dep[100005];
void dfs1(int u,int fa)
{
	f[u]=fa;dep[u]=dep[fa]+1;
	for(int i=head[u];i;i=e[i].nxt)
	{
		int v=e[i].to;
		if(v==fa) continue;
		dfs1(v,u);	
	}
}
int bel[100005],cnt;
int ans;
int tmp[100005];
inline int query(int x,int y)
{
	int res=0;
	while(x!=y)
	{
		if(dep[x]<dep[y]) swap(x,y);
		res|=a[x];x=f[x];
	}
	res|=a[x];
	return res;
}
void dfs(int i)
{
	if(i>n)
	{
		for(int j=1;j<=n;j++) tmp[j]=0;
		for(int j=1;j<=n;j++)		
			for(int k=j;k<=n;k++)
				if(bel[j]==bel[k]) tmp[bel[j]]|=query(j,k);
//		for(int j=1;j<=n;j++) cout<<tmp[j]<<" ";cout<<endl;
		for(int j=1;j<=n;j++) ans=(ans+w[__builtin_popcount(tmp[j])])%mod;
		return;
	}
	for(int j=1;j<=cnt;j++) bel[i]=j,dfs(i+1);
	bel[i]=++cnt;dfs(i+1);--cnt;
}
signed main()
{
	freopen("water.in","r",stdin);
	freopen("water.out","w",stdout);
	read(n,m);
	for(int i=1;i<=m;i++) read(w[i]);
	for(int i=1;i<=n;i++)
	{
		int l;read(l);
		for(int j=1;j<=l;j++)
		{
			int x;read(x);
			a[i]|=(1<<x-1);
		}
	}
	for(int i=1;i<n;i++)
	{
		int u,v;read(u,v);
		add_edge(u,v);add_edge(v,u);
	}
	dfs1(1,0);
	if(n<=10) dfs(1),write(ans);
	flush();
	return 0;
}
